/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2011
 */

package com.nail.zhaogr.service;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import com.nail.zhaogr.model.*;
import com.nail.zhaogr.dao.*;
import com.nail.zhaogr.service.*;
import com.nail.zhaogr.utils.Constants;
import com.nail.zhaogr.vo.query.*;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class CommentsManager extends BaseManager<Comments,java.lang.Long>{

	private CommentsDao commentsDao;
	
	private MembersDao membersDao;
	
	private MyContactDao myContactDao;
	
	private PublishInfoDao publishInfoDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setCommentsDao(CommentsDao dao) {
		this.commentsDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.commentsDao;
	}
	
	public String saveComment(Comments comments,Members memberInfo,int flag,Long myContactId)throws Exception{
		PublishInfo publishInfo = publishInfoDao.getById(comments.getPublishInfoId());
		//comments.setContents(new String(comments.getContents().getBytes("ISO-8859-1"),"utf-8"));
		//只提交过服务态度评价
		if(flag==1){
			Comments temp = getComment(comments);
			temp.setQuality(comments.getQuality());
			temp.setContents(comments.getContents());
			comments = temp;
			quality(comments,publishInfo);
			commentsDao.update(comments);
		}else {
			attitude(comments,publishInfo);
			quality(comments,publishInfo);
			commentsDao.save(comments);
			
			//评论次数增加
			if(publishInfo.getCommentCount()!=null&&!"".equals(publishInfo.getCommentCount())){
				publishInfo.setCommentCount(publishInfo.getCommentCount()+1);
			}else{
				publishInfo.setCommentCount(1);
			}
		}
		
		publishInfo.setLastCommentId(comments.getCommentId());
		//工人获得服务态度评分
		publishInfoDao.update(publishInfo);
		//会员积分增加
		if(memberInfo.getPoints()!=null)
			memberInfo.setPoints(memberInfo.getPoints()+1);
		else
			memberInfo.setPoints(1);
		
		//如果一句话评论不为空，会员积分再加两分
		if(comments.getContents()!=null && !"".equals(comments.getContents().trim())){
			memberInfo.setPoints(memberInfo.getPoints()+2);
		}
		membersDao.update(memberInfo);
		MyContact  entity = myContactDao.getById(myContactId);
		entity.setStatus(2);
		myContactDao.update(entity);
		String jsonData;
		if(flag==1){
			jsonData = "您的评论已提交成功";
		}else{
			//修改联系的状态为，邀请
			//myContactManager.
			jsonData = "您已经成功邀请到  "+publishInfo.getName();
		}
		return jsonData;
		
		/*Comments query = new Comments();
		query.setMemberId(comments.getMemberId());
		query.setPublishInfoId(comments.getPublishInfoId());
		List list = commentsDao.getHibernateTemplate().findByExample(query);
		if(list!=null&&list.size()>0){
			
		}*/
	}
	
	public String saveAttitude(Comments comments,Members memberInfo,int flag)throws DataAccessException{
		//flag=3 已评论过工人服务态度
		if(flag!=3){
			commentsDao.save(comments);
		}
		
		//工人获得服务态度评分
		PublishInfo publishInfo = publishInfoDao.getById(comments.getPublishInfoId());
		
		
		if(flag!=3){
			attitude(comments,publishInfo);
			publishInfoDao.update(publishInfo);
			//会员积分增加
			if(memberInfo.getPoints()!=null)
				memberInfo.setPoints(memberInfo.getPoints()+1);
			else
				memberInfo.setPoints(1);
			//
			membersDao.update(memberInfo);
			//更新session中的memberinfo
		}else{
			
		}
		
		String jsonData;
		if(flag==1){
			jsonData = "您的评论已提交成功";
		}else{
			//修改联系的状态为，邀请
			//myContactManager.
			jsonData = "您已经成功邀请到  "+publishInfo.getName();
		}
		return jsonData;
	}
	
	public Comments getComment(Comments comments)throws DataAccessException{
		Comments query = new Comments();
		query.setMemberId(comments.getMemberId());
		query.setPublishInfoId(comments.getPublishInfoId());
		String queryString = "select t from Comments t where t.publishInfoId="+comments.getPublishInfoId() +" and t.memberId = " +comments.getMemberId();
		List list = commentsDao.getHibernateTemplate().find(queryString);
		if(list!=null&&list.size()>0){
			return  (Comments)list.get(0);
		}else {
			return null;
		}
	}
	
	private void attitude(Comments comments,PublishInfo publishInfo){
		switch(comments.getAttitude()){
		case -1:
			if(publishInfo.getAttitude1()!=null)
				publishInfo.setAttitude1(publishInfo.getAttitude1()+1);
			else
				publishInfo.setAttitude1(1);
			break;
		case 1 :
			if(publishInfo.getAttitude2()!=null)
				publishInfo.setAttitude2(publishInfo.getAttitude2()+1);
			else
				publishInfo.setAttitude2(1);
			break;
		case 2 :
			if(publishInfo.getAttitude3()!=null)
				publishInfo.setAttitude3(publishInfo.getAttitude3()+1);
			else
				publishInfo.setAttitude3(1);
			break;
		case 3 :
			if(publishInfo.getAttitude4()!=null)
				publishInfo.setAttitude4(publishInfo.getAttitude4()+1);
			else
				publishInfo.setAttitude4(1);
			break;
		}
		
		if(publishInfo.getAttitude()!=null)
			publishInfo.setAttitude(publishInfo.getAttitude()+comments.getAttitude());
		else
			publishInfo.setAttitude(comments.getAttitude());
		
		
		if(publishInfo.getCredit()!=null){
			publishInfo.setCredit(publishInfo.getCredit()+comments.getAttitude());
		}else{
			publishInfo.setCredit(comments.getAttitude());
		}
		if(publishInfo.getCommentCount()!=null&&!"".equals(publishInfo.getCommentCount())){
			publishInfo.setCommentCount(publishInfo.getCommentCount()+1);
		}else{
			publishInfo.setCommentCount(1);
		}
	}
	
	private void quality(Comments comments,PublishInfo publishInfo){
		switch(comments.getQuality()){
		case -1:
			if(publishInfo.getQuality1()!=null)
				publishInfo.setQuality1(publishInfo.getQuality1()+1);
			else
				publishInfo.setQuality1(1);
			break;
		case 1 :
			if(publishInfo.getQuality2()!=null)
				publishInfo.setQuality2(publishInfo.getQuality2()+1);
			else
				publishInfo.setQuality2(1);
			break;
		case 2 :
			if(publishInfo.getQuality3()!=null)
				publishInfo.setQuality3(publishInfo.getQuality3()+1);
			else
				publishInfo.setQuality3(1);
			break;
		case 3 :
			if(publishInfo.getQuality4()!=null)
				publishInfo.setQuality4(publishInfo.getQuality4()+1);
			else
				publishInfo.setQuality4(1);
			break;
		}
		
		if(publishInfo.getQuality()!=null)
			publishInfo.setQuality(publishInfo.getQuality()+comments.getQuality());
		else
			publishInfo.setQuality(comments.getQuality());
		
		
		if(publishInfo.getCredit()!=null){
			publishInfo.setCredit(publishInfo.getCredit()+comments.getQuality());
		}else{
			publishInfo.setCredit(comments.getQuality());
		}
		
		/*if(publishInfo.getCommentCount()!=null&&!"".equals(publishInfo.getCommentCount())){
			publishInfo.setCommentCount(publishInfo.getCommentCount()+1);
		}else{
			publishInfo.setCommentCount(1);
		}*/
	}
	
	@Transactional(readOnly=true)
	public Page findPage(CommentsQuery query) {
		return commentsDao.findPage(query);
	}
	/**
	 * @param publishInfoDao the publishInfoDao to set
	 */
	public void setPublishInfoDao(PublishInfoDao publishInfoDao) {
		this.publishInfoDao = publishInfoDao;
	}
	/**
	 * @param membersDao the membersDao to set
	 */
	public void setMembersDao(MembersDao membersDao) {
		this.membersDao = membersDao;
	}
	/**
	 * @param myContactDao the myContactDao to set
	 */
	public void setMyContactDao(MyContactDao myContactDao) {
		this.myContactDao = myContactDao;
	}
	
}
